<?php

/* This parses the MySQL needed to create the table and finds the referenced table
    and the foreign column in that table mapped by the foreign key in this table */

$result2 = mysql_query("SHOW CREATE TABLE $tableName");
$table_def = mysql_fetch_row($result2);
$line = $table_def[1];

$regExp  = '#,\s+CONSTRAINT `([^`]*)` FOREIGN KEY \(`([^`]*)`\) '
        . 'REFERENCES (`[^`]*\.)?`([^`]*)` \(`([^`]*)`\)'
        . '( ON DELETE (RESTRICT|CASCADE|SET NULL|NO ACTION))?'
        . '( ON UPDATE (RESTRICT|CASCADE|SET NULL|NO ACTION))?#';
    $matches = array();
    preg_match_all($regExp, $line, $matches, PREG_SET_ORDER);
    foreach ($matches as $match) {
        $ddl[$match[2]] = array(
            'REF_TABLE_NAME'    => $match[4],
            'REF_COLUMN_NAME'   => $match[5],
//http://stackoverflow.com/questions/7451054/performant-way-to-list-foreign-keys-for-a-mysql-table
        );
    }
?>
